<?php
class UserController extends Controller
{
    function actionIndex()
    {
        if ($this->identity->isAuth()) {
            require_once __DIR__ . '/../models/OrderModel.php';
            $model = new OrderModel();
            $orders = $model->getOrdersByUserID($this->identity->getUser()->id);
            $this->view->render('cabinet', array('orders' => $orders));
        }
        else header('Location: /user/login');
    }

    function actionRecovery($token)
    {
        $model = new UserModel();
        $user = $model->getUserByToken($token);
        if (!empty($_POST['password'])) {
            $model->attributes['password_hash'] = md5($_POST['password']);
            $model->attributes['password_token'] = '';
            if ($model->update($user->id))
                header('Location: /');
        }
        if (!empty($user)) {
            $this->view->render('changePassword', array('user' => $user));
        }
    }

    function actionLogin()
    {
        if (!$this->identity->isAuth()) {
            if (!empty($_POST['password']) && $_POST['email']) {
                if ($this->identity->login($_POST['email'], $_POST['password']))
                    header('Location: /user');
            }
            $this->view->render('login');
        }
        else
            header('Location: /user');
    }

}